<?php
/**
 * Created by PhpStorm.
 * Script Name: CheckApp.php
 * Create: 12/31/22 6:09 PM
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\tenant\middleware;

use app\common\constant\Platform;
use support\View;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class CheckApp implements MiddlewareInterface
{
    /**
     * @inheritDoc
     */
    public function process(Request $request, callable $handler): Response
    {
        $controller_layer = explode('/', request()->getController());
        $controller = strtolower($controller_layer[count($controller_layer) - 1]);
        if(($res = self::checkMp($request, $controller)) instanceof \support\Response){
            return  $res;
        }
        if(($res = self::checkMini($request, $controller)) instanceof \support\Response){
            return  $res;
        }
        if(($res = self::checkBot($request, $controller)) instanceof \support\Response){
            return  $res;
        }
        return $handler($request);
    }

    /**
     * 机器人
     * @param $request
     * @param string $controller
     * @return \support\Response
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function checkBot(&$request, $controller = ''){
        if(strpos($controller, 'bot') === 0 && !in_array($controller, ['bot', 'botsetting'])){
            if(! $bot = session(CURRENT_WECHAT)){
                session(['referer_url' => $request->url()]);
                return error('请先选择账号！', url('index/welcome',['type' => Platform::WECHAT]));
            }
            $request->botInfo = $bot;
            session([CURRENT_WECHAT => $bot]); //续期
            View::assign('bot_info', $request->botInfo);
        }
        return $request;
    }

    /**
     * 小程序
     * @param $request
     * @param string $controller
     * @return \support\Response
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function checkMini(&$request, $controller = ''){
        if(strpos($controller, 'mini') === 0 && !in_array($controller, ['mini'])){
            if(! session(CURRENT_MINI)){
                session(['referer_url' => $request->url()]);
                return error('请先选择账号！', url('index/welcome',['type' => Platform::MINI]));
            }
            $request->miniInfo = session(CURRENT_MINI);
            session([CURRENT_MINI => session(CURRENT_MINI)]); //续期
            View::assign('mini_info', $request->miniInfo);
        }
        return $request;
    }

    /**
     * 公众号
     * @param $request
     * @param string $controller
     * @return \support\Response
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function checkMp(&$request, $controller = ''){
        if(strpos($controller, 'mp') === 0 && !in_array($controller, ['mp'])){
            if(! session(CURRENT_MP)){
                session(['referer_url' => $request->url()]);
                return error('请先选择账号！', url('index/welcome',['type' => Platform::MP]));
            }
            $request->mpInfo = session(CURRENT_MP);
            session([CURRENT_MP => session(CURRENT_MP)]); //续期
            View::assign('mp_info', $request->mpInfo);
        }
        return $request;
    }
}